package com.wenan.data
{
	public class StringUtil
	{
		public function StringUtil()
		{
		}
		
		public static function toFieldStr(fields:Array):String
		{
			if (fields != null && fields.length > 0)
			{
				var s:String = fields[0];
				for(var i:int=1;i<fields.length;i++)
				{
					s += "," + fields[i];
				}
				return s;
			}
			return "";
		}
		
		public static function fromFieldStr(str:String):Array
		{
			if (str == null || str.length == 0)
			{
				return [];
			}
			var strArray:Array = str.split(",");
			var fieldArray:Array = new Array();
			for each(var field:String in strArray)
			{
				field = trim(field);
				if (field != null && field.length > 0)
				{
					fieldArray.push(field);	
				}
			}
			return fieldArray;
		}
		
		/**
		 * remove the blankspaces of left and right in targetString
		 * 例如："字段1,  字段2"，用","符号split之后得到两个字符串："字段1"和"  字段2"，那么
		 * "  字段2"到数据库中查找字段肯定是找不到的，此时我们就 需要对这个字符串进行trim处理。
		 */
		public static function trim(targetString:String):String{
			return trimLeft(trimRight(targetString));
		}
		
		/**
		 * remove only the blankspace on targetString's left
		 */
		public static function trimLeft(targetString:String):String{
			if (targetString == null)
			{
				return null;
			}
			var tempIndex:int = 0;
			var tempChar:String = "";
			for(var i:int=0 ; i<targetString.length ; i++){
				tempChar = targetString.charAt(i);
				if(tempChar != " "){
					tempIndex = i;
					break;
				}
			}
			return targetString.substr(tempIndex);
		}
		
		/**
		 * remove only the blankspace on targetString's right
		 */
		public static function trimRight(targetString:String):String{
			if (targetString == null)
			{
				return null;
			}
			var tempIndex:int = targetString.length-1;
			var tempChar:String = "";
			for(var i:int=targetString.length-1 ; i>=0 ; i--){
				tempChar = targetString.charAt(i);
				if(tempChar != " "){
					tempIndex = i;
					break;
				}
			}
			return targetString.substring(0 , tempIndex+1);
		}
	}
}